/*
 * Copyright (c) 2018. QDU 2015 Java1 Team CompilationError
 */

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package team.mai.antexpress.utils;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;

/**
 * @author lenovo
 */
public class Captcha {

	private final String ALPHABET = "ABCDEFGHJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
	private BufferedImage image;
	private String code;

	public Captcha(int width, int height, int lineCnt, double noiseRate) {
		this.setCode();
		this.setImage(width, height, lineCnt, noiseRate);
	}

	public Captcha() {
		this.setCode();
		this.setImage();
	}


	public BufferedImage getImage() {
		return image;
	}

	public void setImage(int width, int height, int lineCnt, double noiseRate) {
		String code = this.code;
		int fontWidth = ((int) (width * 0.95)) / code.length();
		int fontHeight = (int) (height * 1.0);
		int fontSize = Math.min(fontWidth, fontHeight);
		int paddingX = (int) (width * 0.001);
		int paddingY = height - (height - fontSize) / 2;
		BufferedImage buffimg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		Graphics g = buffimg.getGraphics();
		g.setColor(getRandColor(200, 255));
		g.fillRect(0, 0, width, height);
		for (int i = 0; i < lineCnt; i++) {
			int xs = (int) (Math.random() * width);
			int ys = (int) (Math.random() * height);
			int xe = (int) (Math.random() * width);
			int ye = (int) (Math.random() * height);
			g.setColor(getRandColor(1, 255));
			g.drawLine(xs, ys, xe, ye);
		}
		int area = (int) (noiseRate * width * height);
		for (int i = 0; i < area; ++i) {
			int x = (int) (Math.random() * width);
			int y = (int) (Math.random() * height);
			buffimg.setRGB(x, y, (int) (Math.random() * 255));
		}
		Font font = new Font("Lucida Calligraphy", Font.BOLD, fontSize);
		g.setFont(font);

		for (int i = 0; i < code.length(); ++i) {
			String ch = code.substring(i, i + 1);
			g.setColor(getRandColor(1, 199));
			g.drawString(ch, paddingX + fontWidth * i, paddingY);
		}

		this.image = buffimg;
	}

	public void setImage() {
		setImage(100, 45, 6, 0.02);
	}


	public String getCode() {
		return code;
	}

	public void setCode() {
		String code = "";
		for (int i = 0; i < 4; ++i) {
			int index = (int) (Math.random() * ALPHABET.length());
			code = code + ALPHABET.charAt(index);
		}
		code = code.replaceAll("[Oo0]", "o");
		code = code.replaceAll("[l1I]", "l");
		this.code = code;
		System.out.println("New Captcha:" + code);
	}

	public static Color getRandColor(int L, int R) {
		if (L > 255)
			L = 255;
		if (R > 255)
			R = 255;
		if (L < 0)
			L = 0;
		if (R < 0)
			R = 0;
		int interval = R - L;
		int r = L + (int) (Math.random() * interval);
		int g = L + (int) (Math.random() * interval);
		int b = L + (int) (Math.random() * interval);
		return new Color(r, g, b);
	}

}
